package com.mr.sell.controller;

import com.mr.sell.entity.OrderMaster;
import com.mr.sell.exception.CustomException;
import com.mr.sell.service.OrderService;
import com.mr.sell.utils.ConstantEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Class: SellerOrderController
 * User (作者):MRui
 * TODO (描述)：买家端订单
 * Date 2018.09.27 23:13
 */
@Slf4j
@RestController
@RequestMapping("/seller/order")
public class SellerOrderController {

    @Autowired
    private OrderService orderService;
    /**
    * User (作者): MRui
    * TODO (描述)：订单列表
    * Data：2018.09.27 23:15
    */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "size",defaultValue = "1") int size,
                             Map<String,Object> map){
        PageRequest request = PageRequest.of(page - 1, size);

        Page<OrderMaster> orderList = orderService.getOrderList(request);

        map.put("orderDTOPage",orderList);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("order/list");
    }
    /**
    * User (作者): MRui
    * TODO (描述)：卖家取消订单
    * Data：2018.09.29 22:15
    */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){
        try{
            OrderMaster orderMaster = orderService.getOrder(orderId);
            orderService.cancelOrder(orderMaster);
        }catch (CustomException e){
            log.error("[卖家端取消订单] 查询不到订单");
            map.put("msg", ConstantEnum.EXCEPTION_ORDER_NOT_EXIST.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error",map);
        }
        map.put("msg", ConstantEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success");
    }
    /**
    * User (作者): MRui
    * TODO (描述)：订单详情
    * Data：2018.09.29 22:36
    */
    @GetMapping("detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){
        OrderMaster orderMaster = new OrderMaster();
        try{
            orderMaster = orderService.getOrder(orderId);
            //orderService.cancelOrder(orderMaster);
        }catch (CustomException e){
            log.error("[订单详情] 查询不到订单");
            map.put("msg", ConstantEnum.EXCEPTION_ORDER_NOT_EXIST.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error",map);
        }
        map.put("orderDTO", orderMaster);
        return new ModelAndView("order/detail");
    }

    /**
    * User (作者): MRui
    * TODO (描述)：完结订单
    * Data：2018.09.29 23:03
    */
    @GetMapping("/finished")
    public ModelAndView finished(@RequestParam("orderId") String orderId,
                                 Map<String,Object> map){
        try{
            OrderMaster orderMaster = orderService.getOrder(orderId);
            orderService.finishOrder(orderMaster);
        }catch (CustomException e){
            log.error("[卖家端完结订单] 查询不到订单");
            map.put("msg", ConstantEnum.EXCEPTION_ORDER_NOT_EXIST.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error",map);
        }
        map.put("msg", ConstantEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success");
    }
}
